//
//  Date.cpp
//  日期类的实现
//
//  Created by 卜绎皓 on 2022/9/20.
//

#include "Date.hpp"


Date Date::operator+(int day) const
{
    //Date temp(*this);
    //temp._day += day;
    //while (temp._day > GetMonthDay(temp._year, temp._month))
    //{
    //    temp._day -= GetMonthDay(temp._year,temp._month);
    //    ++temp._month;
    //    if (temp._month == 13)
    //    {
    //        ++temp._year;
    //        temp._month = 1;
    //    }
    //}
    //return temp;

    //相同逻辑太多，直接复用operator+= (这个比较好)
    Date temp = *this;
    temp += day;
    return temp;
}
Date& Date::operator+=(int day)
{
    if (day < 0)//d1 + -100;
    {
        return *this -= -day;//+= -100到-= 100
    }
    _day += day;
    //日期不合法，需要进位
    while (_day > GetMonthDay(_year, _month))
    {
        _day -= GetMonthDay(_year, _month);
        ++_month;
        if (_month == 13)
        {
            ++_year;
            _month = 1;
        }
    }
    return *this;

    //相同逻辑太多，直接复用operator+
    //*this = *this + day;
    //return *this;
}
Date Date::operator-(int day) const
{
    Date temp = (*this);
    temp -= day;
    return temp;
}
Date& Date::operator-=(int day)
{
    if (day < 0)
    {
        return *this += -day;
    }
    _day -= day;
    while (_day < 1)
    {
        --_month;
        if (_month == 0)
        {
            --_year;
            _month = 12;
        }
        _day += GetMonthDay(_year, _month);
    }
    return *this;
}
Date& Date::operator++()
{
    *this += 1;//复用operator+=
    return *this;//返回++后的值
}
Date Date::operator++(int)
{
    Date temp = *this;
    *this += 1;//复用operator+=
    return temp;//返回++前的值
}
Date& Date::operator--()
{
    *this -= 1;//复用operator-=
    return *this;//返回--后的值
}
Date Date::operator--(int)
{
    Date temp(*this);
    *this -= 1;//复用operator-=
    return temp;//返回--前的值
}
int Date::operator-(const Date& d) const
{
    //比较大小
    Date max = *this, min = d;
    int flag = 1;
    if (*this < d)
    {
        max = d;
        min = *this;
        flag = -1;
    }

    int n = 0;
    while (min != max)
    {
        ++min;//利用operator++()
        ++n;
    }
    return n * flag;
}

